package org.teamh.gui.lobby;

import de.lessvoid.nifty.controls.ListBox.ListBoxViewConverter;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;

public class GameBoxViewConverter implements ListBoxViewConverter<GameEntryModel> {
	private static final String NAME = "#name";
    private static final String MAP = "#map";
    private static final String PLAYER = "#player";
    private static final String STATUS = "#status";
    
	@Override
	public void display(Element element, final GameEntryModel model) {
		final Element name = element.findElementByName(NAME);
		final Element map = element.findElementByName(MAP);
		final Element player = element.findElementByName(PLAYER);
		final Element status = element.findElementByName(STATUS);
		
        final TextRenderer textRendererName = name.getRenderer(TextRenderer.class);
        final TextRenderer textRendererMap = map.getRenderer(TextRenderer.class);
        final TextRenderer textRendererPlayer = player.getRenderer(TextRenderer.class);
        final TextRenderer textRendererStatus = status.getRenderer(TextRenderer.class);
        
        if(model != null) {
        	setText(textRendererName, name.getWidth(), model.getName());
        	setText(textRendererMap, map.getWidth(), model.getMap());
        	setText(textRendererPlayer, player.getWidth(), model.getPlayer());
        	setText(textRendererStatus, status.getWidth(), model.getStatus());
        }
	}
	
	private void setText(TextRenderer renderer, int width, String text) {
		renderer.setText(text);
		
		while(renderer.getTextWidth() > width) {
			text = text.substring(0, text.length() - 1);
			renderer.setText(text);
		}
	}

	@Override
	public int getWidth(Element element, GameEntryModel model) {
		
		return 200;
	}

}
